<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Returns the Legacy Extended JSON representation of a BSON value</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.mongodb.bson-tocanonicalextendedjson.html">« MongoDB\BSON\toCanonicalExtendedJSON</a></li>
      <li style="float: right;"><a href="function.mongodb.bson-tophp.html">MongoDB\BSON\toPHP »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.bson.functions.html">函数</a></li>
    <li>Returns the Legacy Extended JSON representation of a BSON value</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.mongodb.bson-tojson" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoDB\BSON\toJSON</h1>
  <p class="verinfo">(mongodb &gt;=1.0.0)</p><p class="refpurpose"><span class="refname">MongoDB\BSON\toJSON</span> &mdash; <span class="dc-title">Returns the Legacy Extended JSON representation of a BSON value</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.mongodb.bson-tojson-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>MongoDB\BSON\toJSON</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$bson</code></span>): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   Converts a BSON string to its
   <a href="https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/" class="link external">&raquo;&nbsp;Legacy Extended JSON</a>
   representation.
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    There exist several JSON formats for representing BSON. This function
    implements the &quot;strict mode&quot; defined in
    <a href="https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/" class="link external">&raquo;&nbsp;MongoDB Extended JSON</a>,
    which has been superseded by the canonical and relaxed formats defined in
    the
    <a href="https://github.com/mongodb/specifications/blob/master/source/extended-json.rst" class="link external">&raquo;&nbsp;Extended JSON Specification</a>
    and implemented by <span class="function"><a href="function.mongodb.bson-tocanonicalextendedjson.html" class="function">MongoDB\BSON\toCanonicalExtendedJSON()</a></span>
    and <span class="function"><a href="function.mongodb.bson-torelaxedextendedjson.html" class="function">MongoDB\BSON\toRelaxedExtendedJSON()</a></span>, respectively.
   </span>
  </p></blockquote>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="simpara">
    <a href="http://www.json.org/" class="link external">&raquo;&nbsp;JSON</a> does not support
    <a href="language.types.float.html#language.types.float.nan" class="link"><strong><code>NAN</code></strong></a> and
    <a href="function.is-infinite.html" class="link"><strong><code>INF</code></strong></a> and
    MongoDB&#039;s Legacy Extended JSON format does not define an alternative
    representation for these values
    (<a href="https://github.com/mongodb/mongo-c-driver/tree/master/src/libbson" class="link external">&raquo;&nbsp;libbson</a> will output
    <code class="literal">nan</code> and <code class="literal">inf</code> literals, which may not be
    parsed as valid JSON). If you are working with BSON that may contain
    non-finite numbers, please use
    <span class="function"><a href="function.mongodb.bson-tocanonicalextendedjson.html" class="function">MongoDB\BSON\toCanonicalExtendedJSON()</a></span> or
    <span class="function"><a href="function.mongodb.bson-torelaxedextendedjson.html" class="function">MongoDB\BSON\toRelaxedExtendedJSON()</a></span>.
   </p>
  </div>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.mongodb.bson-tojson-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">bson</code> (<span class="type">string</span>)</dt>

    <dd>

     <p class="para">
      BSON value to be converted.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mongodb.bson-tojson-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   The converted JSON value.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.mongodb.bson-tojson-errors">
  <h3 class="title">错误／异常</h3>
  <ul class="simplelist">
   <li class="member">Throws <span class="classname"><a href="class.mongodb-driver-exception-unexpectedvalueexception.html" class="classname">MongoDB\Driver\Exception\UnexpectedValueException</a></span> if the input did not contain exactly one BSON document. Possible reasons include, but are not limited to, invalid BSON, extra data (after reading one BSON document), or an unexpected <a href="https://github.com/mongodb/mongo-c-driver/tree/master/src/libbson" class="link external">&raquo;&nbsp;libbson</a> error.</li>
  </ul>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mongodb.bson-tojson-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1370">
   <p><strong>示例 #1 <span class="function"><strong>MongoDB\BSON\toJSON()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$documents&nbsp;</span><span style="color: #007700">=&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'null'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">null&nbsp;</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'boolean'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'string'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'foo'&nbsp;</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'int32'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">123&nbsp;</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'int64'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">4294967295&nbsp;</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'double'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1.0</span><span style="color: #007700">,&nbsp;],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'nan'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">NAN&nbsp;</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'pos_inf'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">INF&nbsp;</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'neg_inf'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;-</span><span style="color: #0000BB">INF&nbsp;</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'array'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;[&nbsp;</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bar'&nbsp;</span><span style="color: #007700">]],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'document'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;[&nbsp;</span><span style="color: #DD0000">'foo'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'bar'&nbsp;</span><span style="color: #007700">]],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'oid'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">ObjectId</span><span style="color: #007700">(</span><span style="color: #DD0000">'56315a7c6118fd1b920270b1'</span><span style="color: #007700">)&nbsp;],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'dec128'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Decimal128</span><span style="color: #007700">(</span><span style="color: #DD0000">'1234.5678'</span><span style="color: #007700">)&nbsp;],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'binary'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Binary</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Binary</span><span style="color: #007700">::</span><span style="color: #0000BB">TYPE_GENERIC</span><span style="color: #007700">)&nbsp;],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'date'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">UTCDateTime</span><span style="color: #007700">(</span><span style="color: #0000BB">1445990400000</span><span style="color: #007700">)&nbsp;],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'timestamp'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Timestamp</span><span style="color: #007700">(</span><span style="color: #0000BB">1234</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5678</span><span style="color: #007700">)&nbsp;],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'regex'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Regex</span><span style="color: #007700">(</span><span style="color: #DD0000">'pattern'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'i'</span><span style="color: #007700">)&nbsp;],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'code'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Javascript</span><span style="color: #007700">(</span><span style="color: #DD0000">'function()&nbsp;{&nbsp;return&nbsp;1;&nbsp;}'</span><span style="color: #007700">)&nbsp;],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'code_ws'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Javascript</span><span style="color: #007700">(</span><span style="color: #DD0000">'function()&nbsp;{&nbsp;return&nbsp;a;&nbsp;}'</span><span style="color: #007700">,&nbsp;[</span><span style="color: #DD0000">'a'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">])&nbsp;],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'minkey'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">MinKey&nbsp;</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;</span><span style="color: #DD0000">'maxkey'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">MaxKey&nbsp;</span><span style="color: #007700">],<br />];<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$documents&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$document</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bson&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">fromPHP</span><span style="color: #007700">(</span><span style="color: #0000BB">$document</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">toJSON</span><span style="color: #007700">(</span><span style="color: #0000BB">$bson</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
{ &quot;null&quot; : null }
{ &quot;boolean&quot; : true }
{ &quot;string&quot; : &quot;foo&quot; }
{ &quot;int32&quot; : 123 }
{ &quot;int64&quot; : 4294967295 }
{ &quot;double&quot; : 1.0 }
{ &quot;nan&quot; : nan }
{ &quot;pos_inf&quot; : inf }
{ &quot;neg_inf&quot; : -inf }
{ &quot;array&quot; : [ &quot;foo&quot;, &quot;bar&quot; ] }
{ &quot;document&quot; : { &quot;foo&quot; : &quot;bar&quot; } }
{ &quot;oid&quot; : { &quot;$oid&quot; : &quot;56315a7c6118fd1b920270b1&quot; } }
{ &quot;dec128&quot; : { &quot;$numberDecimal&quot; : &quot;1234.5678&quot; } }
{ &quot;binary&quot; : { &quot;$binary&quot; : &quot;Zm9v&quot;, &quot;$type&quot; : &quot;00&quot; } }
{ &quot;date&quot; : { &quot;$date&quot; : 1445990400000 } }
{ &quot;timestamp&quot; : { &quot;$timestamp&quot; : { &quot;t&quot; : 5678, &quot;i&quot; : 1234 } } }
{ &quot;regex&quot; : { &quot;$regex&quot; : &quot;pattern&quot;, &quot;$options&quot; : &quot;i&quot; } }
{ &quot;code&quot; : { &quot;$code&quot; : &quot;function() { return 1; }&quot; } }
{ &quot;code_ws&quot; : { &quot;$code&quot; : &quot;function() { return a; }&quot;, &quot;$scope&quot; : { &quot;a&quot; : 1 } } }
{ &quot;minkey&quot; : { &quot;$minKey&quot; : 1 } }
{ &quot;maxkey&quot; : { &quot;$maxKey&quot; : 1 } }
</pre></div>
   </div>
  </div>
 </div>



 <div class="refsect1 seealso" id="refsect1-function.mongodb.bson-tojson-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="function.mongodb.bson-fromjson.html" class="function" rel="rdfs-seeAlso">MongoDB\BSON\fromJSON()</a> - Returns the BSON representation of a JSON value</span></li>
   <li class="member"><span class="function"><a href="function.mongodb.bson-tocanonicalextendedjson.html" class="function" rel="rdfs-seeAlso">MongoDB\BSON\toCanonicalExtendedJSON()</a> - Returns the Canonical Extended JSON representation of a BSON value</span></li>
   <li class="member"><span class="function"><a href="function.mongodb.bson-torelaxedextendedjson.html" class="function" rel="rdfs-seeAlso">MongoDB\BSON\toRelaxedExtendedJSON()</a> - Returns the Relaxed Extended JSON representation of a BSON value</span></li>
   <li class="member"><a href="https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/" class="link external">&raquo;&nbsp;MongoDB Extended JSON</a></li>
   <li class="member"><a href="https://www.mongodb.com/docs/manual/reference/bson-types/" class="link external">&raquo;&nbsp;MongoDB BSON</a></li>
  </ul>
 </div>


</div></div></div></body></html>